package com.ebt.m.customer.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.util.AttributeSet;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import com.ebt.m.R;
import com.ebt.m.commons.buscomponent.listview.IView;
import com.ebt.m.commons.utils.NetUtils;
import com.ebt.m.customer.CustomerUtil;
import com.ebt.m.customer.constant.CustomerConstant;
import com.ebt.m.customer.entity.CustomerSimple;
import com.ebt.m.customer.event.EventACustomerChoosen;
import com.ebt.m.customer.ui.ActivityCustomerAll;
import com.ebt.m.customer.ui.PolicyEditActivity;
import com.ebt.m.customer.util.CustomerNavigator;
import com.ebt.m.proposal_v2.utils.ProposalUtils;
import com.ebt.m.utils.DateUtils;
import com.jakewharton.rxbinding2.view.RxView;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 30天活动客户itemview
 *
 * @author Damon.Han
 */
public class CustomerItemActive extends IView implements PopupMenu.OnMenuItemClickListener {

    private TextView mMessageView;
    private TextView mNameView;
    private TextView mTimeView;
    private ImageView mAvatarView;
    private CustomerSimple mCustomerActive;
    private View mMenuView;

    public void setFrom(int from) {
        this.from = from;
    }

    private int from;

    public CustomerItemActive(Context context) {
        this(context, null);
    }

    @Override
    public void update(Object... args) {
        mCustomerActive = (CustomerSimple) args[0];
        if (mCustomerActive == null) {
            return;
        }
        mNameView.setText(mCustomerActive.getName());
        mMessageView.setText(getMessageByType(mCustomerActive.activeType));
        CustomerUtil.setAvatarByUrlAndSex(getContext(), mAvatarView, mCustomerActive.portraitPath, mCustomerActive.sex);
        if (mCustomerActive.getActiveTime() != null) {
            mTimeView.setText(DateUtils.dateTime2String(DateUtils.parseTimestampInSecondToDate(mCustomerActive.getActiveTime()), DateUtils.YYYY_MM_DD));
        }
        if (from == CustomerUtil.FROM_CHOOSE) {
            mMenuView.setVisibility(GONE);
        }
    }

    public CustomerItemActive(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomerItemActive(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View contentView = inflate(context, R.layout.view_customer_item_active, this);
        mMessageView = (TextView) contentView.findViewById(R.id.tv_message);
        mTimeView = (TextView) contentView.findViewById(R.id.tv_time);
        mNameView = (TextView) contentView.findViewById(R.id.tv_name);
        mAvatarView = (ImageView) contentView.findViewById(R.id.iv_avatar);
        mMenuView = contentView.findViewById(R.id.fl_menu);

        RxView.clicks(contentView.findViewById(R.id.rl_item_view))
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(aVoid -> {
                    if (NetUtils.isNetworkAvailable(getContext().getApplicationContext())) {
                        if (from == CustomerUtil.FROM_OTHER) {
                            Intent intent = new Intent(getContext(), ActivityCustomerAll.class);
                            intent.putExtra(CustomerConstant.CUSTOMER_DETAIL_MODE, CustomerConstant.CUSTOMER_DETAIL_MODE_EDIT);
                            intent.putExtra(CustomerConstant.CUSTOMER_UUID, mCustomerActive.getCustomerId());
                            intent.putExtra(CustomerConstant.CustomerAll.VIEW_PAGER_INDEX, CustomerConstant.CustomerAll.VIEW_PAGER_INDEX_PROFILE);
                            ActivityCompat.startActivity(getContext(), intent, ActivityOptionsCompat.makeSceneTransitionAnimation((Activity) getContext(), mAvatarView, "translate_view").toBundle());

                        } else if (from == CustomerUtil.FROM_CHOOSE) {//选择客户
                            EventACustomerChoosen eventACustomerChoosen = new EventACustomerChoosen();
                            eventACustomerChoosen.customerActive = mCustomerActive;
                            EventBus.getDefault().post(eventACustomerChoosen);
                            ((Activity) getContext()).finish();

                        }
                    } else {
                        Toast.makeText(getContext().getApplicationContext(), getContext().getString(R.string.network_fail), Toast.LENGTH_SHORT).show();
                    }
                });
        RxView.clicks(mMenuView)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(aVoid -> {
                    popMenu();
                });

    }

    private void dismissPopWindow() {
        if (mPopupMenu != null) {
            mPopupMenu.dismiss();
        }
    }

    private void pressProposal() {
        Map<String, String> map = new HashMap<String, String>();
        map.put(CustomerConstant.CUSTOMER_UUID, mCustomerActive.customerId);
        ProposalUtils.makeProposalWithCustomer(getContext(), mCustomerActive.customerId);

    }

    private void pressInsurance() {
        Intent createIntent = new Intent(getContext(), PolicyEditActivity.class);
        Bundle data = new Bundle();
        data.putInt(PolicyEditActivity.ARG_FLAG, 0);
        data.putString(PolicyEditActivity.ARG_CUSTOMER_ID, mCustomerActive.customerId);
        createIntent.putExtras(data);
        getContext().startActivity(createIntent);
        ((Activity) getContext()).overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }

    private void pressNote() {
        CustomerNavigator.startActivityCustomerAllInEdit(
                this.getContext(), mCustomerActive.customerId, 2);
    }

    private void pressAttachment() {
        CustomerNavigator.startActivityCustomerAllInEdit(
                this.getContext(), mCustomerActive.customerId, 3);
    }

    private PopupMenu mPopupMenu;

    private void popMenu() {
        if (mPopupMenu == null) {
            mPopupMenu = new PopupMenu(getContext(), mMenuView);
            mPopupMenu.setOnMenuItemClickListener(this);
            mPopupMenu.inflate(R.menu.menu_active_customer);
        }
        mPopupMenu.show();
    }

    private String getMessageByType(Integer type) {
        return CustomerConstant.InteractionType.getShowText(type);
    }

    @Override
    public boolean onMenuItemClick(MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case R.id.item_proposal:
                pressProposal();
                break;
            case R.id.item_insurance:
                pressInsurance();
                break;
            case R.id.item_note:
                pressNote();
                break;
            case R.id.item_attachment:
                pressAttachment();
                break;
        }
        dismissPopWindow();
        return false;
    }


}